#include <avr/io.h>
#include <math.h>
#include "ses/ses_display_print.h"
#include "ses/ses_scheduler.h"
#include "ses/ses_adc.h"
#include "adc_measure.h"


void startReadLight() {
	adc_asyncRead(ADC_LIGHT_CH, cb_displayLight);
}

void cb_displayLight(uint16_t adcValue) {
	displayPrint_setCursor(0, 0);
	printf("Light: %u    ", adcValue);

	/* Do next job */
	adc_asyncRead(ADC_TEMP_CH, cb_displayTemp);
}

void cb_displayTemp(uint16_t adcValue) {
	uint16_t adcResult;

	/* Convert to Celsius */
	adcResult = adc_convertTemp(adcValue);

	displayPrint_setCursor(0, 1);
	printf("Temp: %u,%u Grad    ", adcResult / 10, adcResult % 10);

	/* do next job */
	adc_asyncRead(ADC_JOYSTICK_CH, cb_displayJoystick);
}


void cb_displayJoystick(uint16_t adcValue) {

	displayPrint_setCursor(0, 2);
	/* Check in which direction the joystick was pushed and show on lcd. */
	if (abs(JOYSTICK_NOMOVE_VAL - adcValue) < JOYSTICK_TOLERANCE) {
		printf("Joystick: ----  ");
	} else if (abs(JOYSTICK_DOWN_VAL - adcValue) < JOYSTICK_TOLERANCE) {
		printf("Joystick: DOWN  ");
	} else if (abs(JOYSTICK_LEFT_VAL - adcValue) < JOYSTICK_TOLERANCE) {
		printf("Joystick: LEFT  ");
	} else if (abs(JOYSTICK_UP_VAL - adcValue) < JOYSTICK_TOLERANCE) {
		printf("Joystick: UP    ");
	} else if (abs(JOYSTICK_RIGHT_VAL - adcValue) < JOYSTICK_TOLERANCE) {
		printf("Joystick: RIGHT ");
	} else{
		printf("Joystick: ----  ");
	}

	/* redo job every ADC_MEASURE_DELAY */
	scheduler_add(startReadLight, ADC_MEASURE_DELAY);
}

